//
//  FTTextFieldCell.m
//  WebPDA
//
//  Created by Justin Yip on 8/15/10.
//  Copyright 2010 TenNights.com. All rights reserved.
//

#import "FTTextFieldCell.h"

@implementation FTTextFieldCell
@synthesize textField;

#pragma mark -
#pragma mark Cell lifecycle

-(id)initWithBundle:(NSBundle*)bundle valueHolder:(id)object reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithBundle:bundle valueHolder:object reuseIdentifier:reuseIdentifier])) {
        // Initialization code
		textField = [[UITextField alloc] initWithFrame:CELL_APPEND_RECT];
		textField.returnKeyType = UIReturnKeyDone;
		textField.borderStyle = UITextBorderStyleNone;
		textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
		[self.contentView addSubview:textField];
    }
    return self;
}

-(void)configureWithObject:(id)kvcObject preferenceSpecifier:(NSDictionary*)ps {
	
	[super configureWithObject:kvcObject preferenceSpecifier:ps];
	
	[textField addTarget:self action:@selector(editingDidEndOnExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
	
	/*
	 mark as secure
	 */
	if((CFBooleanRef)[ps objectForKey:@"IsSecure"] == kCFBooleanTrue){
		
		textField.secureTextEntry = YES;
	}else {
		
		textField.text = [ps objectForKey:@"DefaultValue"];
	}
	
	/*
	 set value from entity
	 */
	//getter selector
	NSString *key = [self keyFromPreferenceSpecifier];
	
	if (key && [kvcObject containsValueForKey:key]) {
		textField.text = [kvcObject valueForKey:key];
	}
	
	textField.keyboardType = UIKeyboardTypeAlphabet;
	if([ps objectForKey:@"KeyboardType"] != nil){
		
		if([[ps objectForKey:@"KeyboardType"] isEqualToString:@"Alphabet"]){
			
			textField.keyboardType = UIKeyboardTypeAlphabet;
		}
		else if([[ps objectForKey:@"KeyboardType"] isEqualToString:@"NumberPad"]){
			
			textField.keyboardType = UIKeyboardTypeNumberPad;
		}
		else if([[ps objectForKey:@"KeyboardType"] isEqualToString:@"NumbersAndPunctuation"]){
			
			textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
		}
		else if([[ps objectForKey:@"KeyboardType"] isEqualToString:@"URL"]){
			
			textField.keyboardType = UIKeyboardTypeURL;
		}
		else if([[ps objectForKey:@"KeyboardType"] isEqualToString:@"EmailAddress"]){
			
			textField.keyboardType = UIKeyboardTypeEmailAddress;
		}
		
	}
	
	textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
	
	if([ps objectForKey:@"AutocapitalizationType"] != nil){
		
		if([[ps objectForKey:@"AutocapitalizationType"] isEqualToString:@"None"]){
			
			textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
		}
		else if([[ps objectForKey:@"AutocapitalizationType"] isEqualToString:@"Sentences"]){
			
			textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
		}
		else if([[ps objectForKey:@"AutocapitalizationType"] isEqualToString:@"Words"]){
			
			textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
		}
		else if([[ps objectForKey:@"AutocapitalizationType"] isEqualToString:@"AllCharacters"]){
			
			textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
		}
	}
	
	self.textField.autocorrectionType = UITextAutocorrectionTypeDefault;
	
	if([ps objectForKey:@"AutocorrectionType"] != nil){
		
		if([[ps objectForKey:@"AutocorrectionType"] isEqualToString:@"Default"]){
			
			textField.autocorrectionType = UITextAutocorrectionTypeDefault;
		}
		else if([[ps objectForKey:@"AutocorrectionType"] isEqualToString:@"No"]){
			
			textField.autocorrectionType = UITextAutocorrectionTypeNo;
		}
		else if([[ps objectForKey:@"AutocorrectionType"] isEqualToString:@"Yes"]){
			
			textField.autocorrectionType = UITextAutocorrectionTypeYes;
		}
		
	}
}

#pragma mark convert from string to enum value

//-(void)convertKeyboardTypeFromString:(NSString*)keyboardTypeString {
//	static NSString *kbtypeString[] = {@"Alphabet", @"NumberPad", @"NumbersAndPunctuation", @"URL", @"EmailAddress"};
//	static int kbtypeValue[] = {UIKeyboardTypeAlphabet, UIKeyboardTypeNumberPad, UIKeyboardTypeNumbersAndPunctuation, UIKeyboardTypeURL, UIKeyboardTypeEmailAddress};
//	[kbtypeString objectAtIndex:0];
//}

#pragma mark -
#pragma mark Callback

-(id)getValue {
	return textField.text;
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
	[super setEditing:editing animated:animated];
	textField.enabled = editing;
	textField.backgroundColor = editing ? [UIColor grayColor] : [UIColor clearColor];
}

-(void)editingDidEndOnExit:(id)sender {
	[self resignFirstResponder];
}

#pragma mark -
#pragma mark Memory management

- (void)dealloc {
	[textField removeFromSuperview];
	
	[textField release];
    [super dealloc];
}

@end
